package com.bikes.mileagetracker

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.bikes.mileagetracker.entity.TrackingEntity
import com.bikes.mileagetracker.entity.UserEntity
import com.bikes.mileagetracker.service.PersistenceImpl
import kotlinx.android.synthetic.main.activity_log.*
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.OrientationHelper
import android.view.MenuItem


class LogActivity : AppCompatActivity() {

    var trackList = arrayListOf<TrackingEntity>()
    var persistence: PersistenceImpl = PersistenceImpl()
    var user: UserEntity = UserEntity()

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        // TODO Auto-generated method stub
        if(item!!.getItemId() == android.R.id.home)
        {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_log)

        // Init Action bar
        val actionBar = supportActionBar
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true)
            actionBar.setHomeButtonEnabled(true)
        }

        user = MileageTrackerApplication.getCurrentAccount()!!

        trackList.addAll(persistence.loadUserTrackings(user.id))

        val recycleAdapter = MyRecyclerAdapter(this@LogActivity, trackList)
        val layoutManager = LinearLayoutManager(this)
        vw_log_recyclerview.layoutManager = layoutManager
        layoutManager.orientation = OrientationHelper.VERTICAL
        vw_log_recyclerview.adapter = recycleAdapter
    }


}
